/*******************************************************************************
 * Copyright (c) 2006 Ultimate Technology, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Bojan Vukojevic - initial API and implementation
 *******************************************************************************/
package com.ultimatetech.cim.views.content;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

import javax.wbem.cim.CIMException;
import javax.wbem.cim.CIMObjectPath;
import javax.wbem.client.CIMClient;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Preferences;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.ui.PlatformUI;

import com.ultimatetech.cim.preferences.CIMPreferencePage;

public class ClassNames
{

    List classNames = null;
    Preferences pref = null;
    CIMClient client = null;

    public ClassNames(Preferences p)
    {
        classNames = new ArrayList();
        pref = p;
        initData();
    }

    private void initData()
    {
        try
        {
            client = CIMConnect.connect(pref.getString(CIMPreferencePage.PreferenceNames.IS_SECURE),
                    pref.getString(CIMPreferencePage.PreferenceNames.CIM_HOST),
                    pref.getString(CIMPreferencePage.PreferenceNames.CIM_PORT),
                    pref.getString(CIMPreferencePage.PreferenceNames.CIM_NAMESPACE));

            // if client failed to connect, we already show error, but client is
            // empty
            if (client != null)
            {
                String filter = pref.getString(CIMPreferencePage.PreferenceNames.CIM_FILTER);
                Enumeration e = client.enumerateClassNames(new CIMObjectPath(), true);
                while (e.hasMoreElements())
                {
                    CIMObjectPath cop = (CIMObjectPath) e.nextElement();
                    if (filter == null || "".equals(filter) || cop.getObjectName().startsWith(filter))
                    {
                        classNames.add(cop);
                    }
                }
                // sort
                Collections.sort(classNames, new COPComparator());
                client.close();
            }
        }
        catch (CIMException e)
        {
            ErrorDialog ed = new ErrorDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
                    "Connection Error", "Connection failed", new Status(Status.ERROR, "CIMPlugin", e.getXmlCode(
                            e.getID()), e.getMessage(), e), IStatus.ERROR);
            ed.open();
        }
    }

    public List getClassNames()
    {
        return classNames;
    }

    /*
     * public static void main(String args[]){ ClassNames cn = new ClassNames();
     * }
     */

}
